﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;

public partial class controls_admin_listhoadon : System.Web.UI.UserControl
{
    Entity.HoaDon clsE_HoaDon = new VietStar.Library.Entity.HoaDon();
    Business.HoaDon clsB_HoaDon = new VietStar.Library.Business.HoaDon();
    Entity.ChiTietHoaDon clsE_ChiTietHoaDon = new VietStar.Library.Entity.ChiTietHoaDon();
    Business.ChiTietHoaDon clsB_ChiTietHoaDon = new VietStar.Library.Business.ChiTietHoaDon();
    PagedDataSource _PageDataSource = new PagedDataSource();
    private static DataTable dtHoaDon = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtHoaDon = clsB_HoaDon.Select_AllHoaDon(clsE_HoaDon);
            Load_HoaDon();
        }
    }
    private void Load_HoaDon()
    {
        if (dtHoaDon.Rows.Count == 0)
        {
            BindItemsList(dtHoaDon);
            PhanTrang.Visible = true;
            msgNoRows.Style.Add("display", "block");
        }
        else if (dtHoaDon.Rows.Count > 0 && dtHoaDon.Rows.Count <= 3)
        {
            BindItemsList(dtHoaDon);
            PhanTrang.Visible = false;
        }
        else if (dtHoaDon.Rows.Count > 3)
        {
            BindItemsList(dtHoaDon);
            PhanTrang.Visible = true;
        }
    }
#region phân trang.............
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindItemsList(dtHoaDon);
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindItemsList(dtHoaDon);
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindItemsList(dtHoaDon);
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = Convert.ToInt32(ViewState["TotalPages"]) - 1;
        BindItemsList(dtHoaDon);
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            //lnkbtnPage.Style.Add("fone-size", "14px");
            //lnkbtnPage.Font.Bold = true;
            lnkbtnPage.CssClass = "active";
        }
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
            BindItemsList(dtHoaDon);
        }
    }
    private void BindItemsList(DataTable dt)
    {
        DataTable dataTable = dt;
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 3;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.rptDanhSachTinTuc.DataSource = _PageDataSource;
        this.rptDanhSachTinTuc.DataBind();
        this.doPaging();
    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - 5;
        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];

            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
#endregion
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {
        dtHoaDon = clsB_HoaDon.Select_AllHoaDon(clsE_HoaDon);
        CurrentPage = 0;
        Load_HoaDon();
        txtTuKhoa.Text = "Tìm kiếm tên đăng nhập";
        ddlTrangThai.SelectedValue = "2";
    }
    protected void rptDanhSachTinTuc_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "xoa")
            {
                bool delete = false;
                clsE_HoaDon.Id = Convert.ToInt32(e.CommandArgument.ToString());
                clsE_ChiTietHoaDon.MaHD = Convert.ToInt32(e.CommandArgument.ToString());               
                delete = clsB_ChiTietHoaDon.Delete_TheoMaHD(clsE_ChiTietHoaDon);
                delete = false;
                delete = clsB_HoaDon.Delete(clsE_HoaDon);
                if (delete)
                {
                    Alert.Show("Xóa hóa đơn thành công!!!");
                    dtHoaDon = clsB_HoaDon.Select_AllHoaDon(clsE_HoaDon);
                    Load_HoaDon();
                }
                else
                { Alert.Show("Xóa hóa đơn không thành công !!!"); }
            }
            if (e.CommandName == "statusupdate")
            {
                try
                {
                    bool trangthai = false;
                    clsE_HoaDon.Id = Convert.ToInt32(e.CommandArgument.ToString());
                    trangthai = clsB_HoaDon.Update_TrangThai(clsE_HoaDon);
                    if (trangthai)
                    {
                        Alert.Show("Cập nhật trạng thái thành công !!!");
                        dtHoaDon = clsB_HoaDon.Select_AllHoaDon(clsE_HoaDon);
                        Load_HoaDon();
                    }
                    else
                    { Alert.Show("Cập nhật trạng thái không thành công !!!"); }
                }
                catch
                { }
            }
        }
        catch (Exception ex)
        {
 
        }
    }
    private void TimKiem(int Opt)
    {
        clsE_HoaDon.Option = Opt;
        dtHoaDon = clsB_HoaDon.SelectTimKiem(clsE_HoaDon);
        CurrentPage = 0;
        Load_HoaDon();
    }
    protected void btnTim_Click(object sender, EventArgs e)
    {
        clsE_HoaDon.Ten = txtTuKhoa.Text;
        clsE_HoaDon.TrangThai = Convert.ToBoolean(Convert.ToInt32(ddlTrangThai.SelectedValue));
        if (txtTuKhoa.Text != "Tìm kiếm tên đăng nhập" && ddlTrangThai.SelectedValue == "2")
        {
            TimKiem(0);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm tên đăng nhập" && ddlTrangThai.SelectedValue != "2")
        {
            TimKiem(1);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm tên đăng nhập" && ddlTrangThai.SelectedValue != "2")
        {
            TimKiem(2);
        }
        else
        {
            Alert.Show("Bạn chưa nhập từ khóa hoặc chưa chọn trạng thái cần tìm");
            dtHoaDon = clsB_HoaDon.Select_AllHoaDon(clsE_HoaDon);
            CurrentPage = 0;
            Load_HoaDon();
        }
    }
}
